<template>
  <div class="grid-container">
    <div class="box-wrapper">
      <div class="box-wrapper-inner">
        <div class="cn-logo">
          <img
            src="../assets/image/logo-vertical-color-lightCN.2cID7.svg"
            alt=""
          />
        </div>
        <div class="register-input-container">
          <input
            v-model="username"
            type="text"
            placeholder="用户名:4到16位(字母,数字,下划线)"
          />
          <input
            v-model="password"
            type="password"
            placeholder="密码:以字母开头，长度在6~18之间"
          />
          <input v-model="phone" type="number" placeholder="手机号" />
          <input v-model="email" type="email" placeholder="邮箱" />
        </div>
        <div class="submit">
          <button @click="goRegister">注册</button>
        </div>
        <Dengl />
      </div>
    </div>
    <Footer />
  </div>
</template>

<script>
import Dengl from "../components/dengl.vue";
import Footer from "../components/footer.vue";
import { reqRegister } from "../api/index";
export default {
  components: { Footer, Dengl },
  data() {
    return {
      username: "",
      password: "",
      phone: "",
      email: "",
    };
  },
  methods: {
    async goRegister() {
      if (!/^[a-zA-Z0-9_-]{4,16}$/.test(this.username)) {
        this.$toast.fail({ message: "用户名\n格式错误" });
        return;
      }
      if (!/^[a-zA-Z]\w{5,17}$/.test(this.password)) {
        this.$toast.fail({ message: "密码\n格式错误" });
        return;
      }
      if (
        !/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
          this.phone
        )
      ) {
        this.$toast.fail({
          message: "手机号码\n格式错误",
        });
        return;
      }
      if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(this.email)) {
        this.$toast.fail({
          message: "电子邮箱\n格式错误",
        });
        return;
      }
      let obj = {
        username: this.username,
        password: this.password,
        phone: this.phone,
        email: this.email,
      };
      const res = await reqRegister(obj);
      if (res.code == 200) {
        this.$toast("注册成功");
        this.$router.push("/login");
      } else if (res.code == 201) {
        this.$toast.fail("注册失败,用户名已存在");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import url("../assets/css/register.css");
</style>
